package echat.future.cn.echat.my.view;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.zxing.WriterException;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

import echat.future.cn.echat.AppApplication;
import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityInvitationsBinding;
import echat.future.cn.echat.my.utils.ImageLoadUtils;
import echat.future.cn.echat.my.utils.QRCodeUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import echat.future.cn.echat.utils.Util;

public class InvitationsActivity extends BaseActivity<ActivityInvitationsBinding>{


    AdapterNavagation adapterNavagation;

    public static final String EXTRA_TYPE = "EXTRRA_TYPE";
    public static  String url = "";
    public static final int EXTRA_USER = 2;
    public static final int EXTRA_ANCHOR = 1;

    private int type = EXTRA_USER;

    @Override
    public int layoutId() {
        return R.layout.activity_invitations;
    }

    @Override
    public IBaseNavagation navagation() {
        adapterNavagation =  new AdapterNavagation(this).setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
        return adapterNavagation;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        type = getIntent().getIntExtra(EXTRA_TYPE,EXTRA_USER);
        if(type == EXTRA_ANCHOR) {
            adapterNavagation.setTitle("邀请粉丝", 16, R.color.white);
            bind.center.setText("想和我一对一、私密、视频聊天么\n" +
                    "我在E聊等你，快来撩我吧！");
        }else{
            adapterNavagation.setTitle("邀请用户", 16, R.color.white);
            bind.center.setText(" 成为认证用户，预约不等待，\n" +
                    "快来加入吧");
        }
        bind.shareWeixin.setOnClickListener(v -> shareWx());
        url = String.format("http://greatknowledge.com.cn/app/html/eliao.html?id=%s",UserHelper.getUserId());
        bind.shareQq.setOnClickListener(v -> shareQQ());
        ImageLoadUtils.loadImage(this, UserHelper.getUser().getAvatar(),bind.head);
        bind.username.setText(UserHelper.getUser().getUsername()+"");
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        try {
            bind.qrcode.setImageBitmap( QRCodeUtils.encodeAsBitmap(url, bind.qrcode.getWidth(), bind.qrcode.getHeight()));
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    private void shareQQ(){
        final Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "E聊");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "一对一视频真人私密聊天，海量真实美女资源，拒绝没有温度的文字聊天");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  url);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://pp.myapp.com/ma_icon/0/icon_52751178_1539138324/96");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "E聊");
        AppApplication.tencent.shareToQQ(this, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                toast("分享成功");
            }

            @Override
            public void onError(UiError uiError) {
                toast("分享失败");
            }

            @Override
            public void onCancel() {
                toast("取消分享");
            }
        });
    }

    private void shareWx(){
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "E聊";
        msg.description = "一对一视频真人私密聊天，海量真实美女资源，拒绝没有温度的文字聊天";
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        ((AppApplication)(getApplication())).getIwxapi().sendReq(req);
    }
}
